/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.hugo.senai.dao;

import br.com.hugo.senai.entities.Usuario;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrador
 */
public class Hibernate {

    private static Session session = null;

    public static void save(Object o) {
        session = HibernateUtil.getSessionFactory().openSession();

        try {
            session.beginTransaction();
            session.save(o);
            session.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "SALVO.", "RESPOSTA", JOptionPane.INFORMATION_MESSAGE);

        } catch (HibernateException ex) {

            session.getTransaction().rollback();
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERRO ", JOptionPane.ERROR_MESSAGE);

        } finally {
            session.close();
        }
    }

    public static Usuario login(String login, String senha) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Usuario u = (Usuario) session.createQuery("from Usuario where login='" + login + "' and senha='" + senha + "'").uniqueResult();
        session.getTransaction().commit();
        session.close();
        return u;
    }

    public static void update(Object o) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(o);
        session.getTransaction().commit();
        session.close();

    }

    public static void delete(Object o) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(o);
        session.getTransaction().commit();
        session.close();

    }

    public static Object get(Class type, String id) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Object o = session.get(type, id);
        session.getTransaction().commit();
        session.close();
        return o;
    }

    public static Object get(Class type, int id) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Object o = session.get(type, id);
        session.getTransaction().commit();
        session.close();
        return o;
    }

    public static Object load(Class type, int id) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Object o = session.load(type, id);
        session.getTransaction().commit();
        session.close();
        return o;
    }

    public static List listAll(Class type) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List list = session.createCriteria(type).list();
        session.getTransaction().commit();
        session.close();

        return list;
    }

    public static List listAll(Class searchClass, Object theObject, String column) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List list = session.createCriteria(searchClass).add(Restrictions.eq(column, theObject)).list();
        session.getTransaction().commit();
        session.close();
        return list;

    }

    public static List listByQuestion(int idConteudo) {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List list = session.createQuery("from Questao Q GROUP BY Q.id").list();
        session.getTransaction().commit();
        session.close();
        return list;

    }

    public static Object checkLogin(String login) {

        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Object o = session.createQuery("from Usuario where login=:login").setString("login", login).uniqueResult();
        session.getTransaction().commit();
        session.close();
        return o;
    }

}
